package com.example.shop_car_2208.search

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView

import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_car_2208.R
import com.example.shop_car_2208.adapter.GoodsAdapter
import com.example.shop_car_2208.adapter.GoodsMultiAdapter
import com.example.shop_car_2208.adapter.HistoryAdapter
import com.example.shop_car_2208.databinding.ActivitySearchBinding
import com.example.shop_car_2208.db.MyDBUtils
import com.example.shop_car_2208.entity.Data
import com.example.shop_car_2208.entity.SearchEntity
import com.example.shop_car_2208.mvvm.BaseActivity
@Route(path = "/app/search")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsAdapter: GoodsMultiAdapter
    lateinit var historyAdapter: HistoryAdapter
    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if (it.isNotEmpty()){
                if (it.isNotEmpty()){
                    it.forEachIndexed { index, data ->
                        data.type = index%2
                        goodsAdapter.data.add(data)
                    }
                }
                goodsAdapter.data.addAll(it)
                goodsAdapter.notifyDataSetChanged()

            }else{
                ToastUtils.showLong("未搜索到符合条件的数据，换一个关键词试试")
                goodsAdapter.data.clear()
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    @SuppressLint("MissingInflatedId")
    override fun initView() {

        goodsAdapter = GoodsMultiAdapter()
        binding.rv.adapter = goodsAdapter
        binding.rv.layoutManager = GridLayoutManager(this,2)



        //执行搜索功能
        binding.sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击回车时触发
                if(query!=null){
                    viewModel.search(query)
                    //保存搜素记录
                    MyDBUtils.getDB().getSearchDao().insertSearch(SearchEntity(query,System.currentTimeMillis()))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文字发生变化时触
                return true
            }

        })


        //初始化搜索记录
//        historyAdapter = HistoryAdapter()
//        binding.rvSearch.adapter = historyAdapter
//        binding.rvSearch.layoutManager = GridLayoutManager(this,3)
//        historyAdapter.data.clear()
//        historyAdapter.data.addAll(MyDBUtils.getDB().getSearchDao().querySearch())
//        historyAdapter.notifyDataSetChanged()
        val data = MyDBUtils.getDB().getSearchDao().querySearch()
        if (data.isNotEmpty()){
            binding.cg.removeAllViews()
            data.forEach{
                val key  = it.name
                val chip = LayoutInflater.from(this).inflate(R.layout.item_chip,null)
                chip.findViewById<TextView>(R.id.chip).text = it.name//给id为chip赋值
                binding.cg.addView(chip)
                //点击item查询
                chip.setOnClickListener{
                    binding.sv.setQuery(key,true)
                }
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel> {
        return SearchViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_search
    }

}